package com.yuyue.dronesystem.controllers;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.yuyue.dronesystem.entities.Site;
import com.yuyue.dronesystem.enums.PlaceType;
import com.yuyue.dronesystem.repository.SiteRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;



/**
 * SiteController
 */
@RequestMapping(path = "/main")
@Controller
public class SiteController {

	private final String PREFIX = "main/site/";

    @Autowired
    SiteRepository siteRepository;


    // 查询所有坐标返回列表页面
    @GetMapping(value = "/sites")
    public String list(ModelMap modelMap) {
        List<Site> hospitals = siteRepository.findByType(PlaceType.HOSPITAL);
        List<Site> posts = siteRepository.findByType(PlaceType.POST);
        List<Site> ppoints = siteRepository.findByType(PlaceType.POPULATION);
        List<Site> others = siteRepository.findByType(PlaceType.OTHER);
        modelMap.addAttribute("hospitals", hospitals);
        modelMap.addAttribute("posts", posts);
        modelMap.addAttribute("ppoints", ppoints);
        modelMap.addAttribute("others", others);
        return PREFIX+"list";
    }

    // 来到坐标添加页面
    @GetMapping(value = "/site")
    public String toAddPage(ModelMap modelMap) {
        List<PlaceType> types = Arrays.asList(PlaceType.values());
        modelMap.addAttribute("types", types);
        return PREFIX+"add";
    }

    // 添加坐标
    @PostMapping(value = "/site")
    public String addSite(Site site) {
        System.out.println("添加的坐标信息："+site);
        siteRepository.save(site);
        return "redirect:/main/sites";
    }
    
    // 来到坐标修改页面
    @GetMapping(value = "/site/{id}")
    public String toEditPage(@PathVariable Integer id,
                             ModelMap modelMap) {
        Optional<Site> site = siteRepository.findById(id);
        modelMap.addAttribute("site", site.get());
        List<PlaceType> types = Arrays.asList(PlaceType.values());
        modelMap.addAttribute("types", types);
        return PREFIX+"add";
    }

    // 坐标修改，获取坐标id
    @PutMapping(value = "/site")
    public String updateUser(Site site) {
        System.out.println("修改的坐标信息："+site);
        siteRepository.save(site);
        return "redirect:/main/sites";
    }

    // 坐标删除
    @DeleteMapping(value = "/site/{id}")
    public String deleteUser(@PathVariable Integer id) {
        siteRepository.deleteById(id);
        return "redirect:/main/sites";
    }
}